#include "heroplane.h"
#include "config.h"
#include<QSound>

HeroPlane::HeroPlane()
{
    //初始化加载图片资源
    m_Plane.load(HERO_PATH);

    //初始化坐标
    m_X=0;
    m_Y=(GAME_HEIGHT-m_Plane.height())*0.5;

    //初始化矩形边框
    m_Rect.setWidth(m_Plane.width());
    m_Rect.setHeight(m_Plane.height());
    m_Rect.moveTo(m_X,m_Y);
    //初始化奖励状态
    jr=false;
}

void HeroPlane::shoot()
{
    if(!jr){
        //累加时间间隔记录变量
        m_recorder++;

        //如果记录数字未达到发射子弹时间间隔，不发射
        if(m_recorder<BULLET_INTERVAL)
            return;
        //达到发射时间
        m_recorder=0;
        //发射子弹
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(m_bullets[i].m_Free)
            {
                //发射
                m_bullets[i].m_Free=false;
                QSound::play(SOUND_BULLET);
                //设置子弹坐标
                m_bullets[i].m_X=m_Rect.width()-10;
                m_bullets[i].m_Y=m_Y+10;
                break;
            }
        }
    }
    if(jr){
        //累加时间间隔记录变量
        m_recorder++;

        //如果记录数字未达到发射子弹时间间隔，不发射
        if(m_recorder<BULLET_INTERVAL/5)
            return;
        //达到发射时间
        m_recorder=0;
        //发射子弹
        for(int i=0;i<BULLET_NUM;i++)
        {
            if(m_bullets[i].m_Free)
            {
                //发射
                m_bullets[i].m_Free=false;
                QSound::play(SOUND_BULLET);
                //设置子弹坐标
                m_bullets[i].m_X=m_Rect.width()-10;
                m_bullets[i].m_Y=m_Y+10;
                break;
            }
        }
    }
}

void HeroPlane::setposition(int x, int y)
{
    m_X=x;
    m_Y=y;
    m_Rect.moveTo(m_X,m_Y);
}
